##
import matplotlib.pyplot as plt
import random
import numpy as np

x_1 = np.arange(0,12.1,0.1)
y_1 = np.sin(x_1)
plt.plot(x_1,y_1, color='red', linewidth=3,label='sinx')
ax1 = plt.gca()
ax1.set_title('Sin(x)',fontname='Arial',fontsize=20)
ax1.set_ylabel('Y')
ax1.set_xlabel('X')
plt.legend()
# plt.show()

#图2
x_2 = np.arange(0,12.1,0.1)
y_2 = np.cos(x_2)
plt.plot(x_2,y_2, color='blue', linewidth=3)
ax2 = plt.gca()
ax2.set_title('Cos(x)',fontname='Arial',fontsize=20)
ax2.set_ylabel('Y')
ax2.set_xlabel('X')
# plt.show()

#图3
x_3 = np.arange(0,12.1,0.1)
y_3 = np.tan(x_3)
plt.plot(x_3,y_3, color='green', linewidth=3)
ax3 = plt.gca()
ax3.set_title('Tan(x)',fontname='Arial',fontsize=20)
ax3.set_ylabel('Y')
ax3.set_xlabel('X')
# plt.show()

#图4

y_4 = [i + random.random() for i in range(10)]
plt.plot(y_4, color='orange', linewidth=3)
ax4 = plt.gca()
ax4.set_title('Cos(x)',fontname='Arial',fontsize=20)
ax4.set_ylabel('Y')
ax4.set_xlabel('X')
# plt.show()


###
fig, ax = plt.subplots(2,2)
ax[0][0] = ax1
ax[0][1] = ax2
ax[1][0] = ax3
ax[1][1] = ax4

# print(ax)
plt.show()

##  plt.figure 实现

fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax1.plot(y_1,color='#FFB6C1',label='sinx')
ax1.set_title('this is sinx')
ax1.set_xlabel('time/s')
ax1.set_ylabel('A')
ax1.legend(loc='upper right')

ax2 = fig.add_subplot(2, 2, 2)
# ax2.


plt.show()

##  plt.subplots()
import matplotlib.pyplot as plt
fig , axes = plt.subplots(2,2)
axes[0][0].plot(y_1,color='#FFB6C1',label='sinx')
axes[0][0].set_title('this is sinx')
axes[0][0].set_xlabel('time/s')
axes[0][0].set_ylabel('A')
axes[0][0].legend(loc='upper right')
plt.show()

## for 搞定

# 4 张图 : [0][0] [0][1] [1][0] [1][1]
# 0 1 2 3
# 0 1
# 2 3

# 4张图
for idx in range(4):
    axes[idx//2][idx%2]

# 9张图
for idx in range(9):
    axes[idx//3][idx%3]

## 总结：画一张 n*n 的图
import matplotlib.pyplot as plt
n = 2
N = 4
y_list = [y_1,y_2,y_3,y_4]

for idx in range(N):
    axes[idx // n][idx % n].plot(y_list[idx])

plt.show()



